<?php


namespace app\admin\controller\School;


use app\admin\controller\BaseController;
use app\admin\model\School\SchoolAreaModel;
use app\Utils\Util;
use think\Request;

class SchoolArea extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getSchoolAreaList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $list = SchoolAreaModel::getSchoolAreaListWhere($page, $limit, $where);
        $count = SchoolAreaModel::getSchoolAreaCountWhere($where);
        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");
    }

    /**
     * 获取更多
     * @param Request $request
     * @return array
     */
    public function getSchoolAreaMore(Request $request)
    {
        $list = SchoolAreaModel::getSchoolAreaWhereMore([['is_show','=',1]]);
        return Util::returnJson(self::CODE_DATA, $list);
    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addSchoolArea(Request $request)
    {
        $param = $request->param();
        $area_name = !empty($param['area_name']) ? $param['area_name'] : "";
        $sort = !empty($param['sort']) ? $param['sort'] : 0;
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        $data = [
            'area_name' => $area_name,
            'sort' => $sort,
            'up_time' => date("Y-m-d H:i:s",time()),
            'add_time' => date("Y-m-d H:i:s",time())
        ];

        if (!empty($is_show) && $is_show == "on") {
            $data['is_show'] = 1;
        }else{
            $data['is_show'] = 0;
        }
        $id = SchoolAreaModel::addSchoolArea($data);
        if ($id) {
            return Util::returnJson(self::CODE_SUCCESS, $id, "添加成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editSchoolArea(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $area_name = !empty($param['area_name']) ? $param['area_name'] : "";
        $sort = !empty($param['sort']) ? $param['sort'] : 0;
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if (empty($id)) {
            return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        }

        $save = [
            'area_name' => $area_name,
            'sort' => $sort,
            'up_time' => date("Y-m-d H:i:s",time())
        ];

        if (!empty($is_show) && $is_show == "on") {
            $save['is_show'] = 1;
        }else{
            $save['is_show'] = 0;
        }

        $flag = SchoolAreaModel::updateSchoolAreaWhere([['id', '=', $id]], $save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败");
        }
    }

}